/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import etcore.webapp.grouptalk.common.dao.BaseDAO;
import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;

/**
 * BaseService implements common transaction operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */

public class BaseService {
	/**
	 * Transaction of deleting instances
	 * 
	 * @param persistentInstanceList
	 *            instances to be deleted
	 */
	@SuppressWarnings("rawtypes")
	public void deleteList(List  persistentInstanceList) {

		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			BaseDAO dao = new BaseDAO();
			dao.deleteList(persistentInstanceList);
			tx.commit();

		} catch (RuntimeException re) {

			tx.rollback();
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Transaction of deleting an instance
	 * 
	 * @param persistentInstance
	 *            an instance to be deleted
	 */
	public void deleteSingle(Object persistentInstance) {

		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			BaseDAO dao = new BaseDAO();
			dao.deleteSingle(persistentInstance);
			tx.commit();

		} catch (RuntimeException re) {

			tx.rollback();
		} finally {
			if (session != null)
				session.close();
		}
	}

	/**
	 * Transaction of merging an instance
	 * 
	 * @param detachedInstance
	 *            an instance to be merged
	 * @return
	 */
	public Object merge(Object detachedInstance) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			BaseDAO dao = new BaseDAO();
			Object result = dao.merge(detachedInstance);
			tx.commit();
			return result;

		} catch (RuntimeException re) {

			tx.rollback();
		} finally {
			if (session != null) {
				session.flush();
				session.close();
			}
		}
		return null;
	}

	/**
	 * Transaction of saving an instance
	 * 
	 * @param transientInstance
	 *            an instance
	 */
	public void save(Object transientInstance) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			BaseDAO dao = new BaseDAO();
			dao.save(transientInstance);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
}
